﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Calendar;

namespace ProgLab.CalendarSyncUtil
{
    public interface ICalendarItem
    {
        string Subject
        {
            get;
            set;
        }

        string Location
        {
            get;
            set;
        }

        string Content
        {
            get;
            set;
        }

        DateTime Start
        {
            get;
            set;
        }

        DateTime End
        {
            get;
            set;
        }

        bool IsAllDay
        {
            get;
            set;
        }

        bool IsSameEvent(ICalendarItem rhs);
    }

    public class CalendarItem : ICalendarItem
    {
        protected string subject = "";
        protected string location = "";
        protected string content = "";
        protected DateTime start = DateTime.MinValue;
        protected DateTime end = DateTime.MinValue;
        protected bool isAllDay = false;


        #region ICalendarItem 成員

        public string Subject
        {
            get
            {
                return subject;
            }
            set
            {
                subject = value;
            }
        }

        public string Location
        {
            get
            {
                return location;
            }
            set
            {
                location = value;
            }
        }

        public string Content
        {
            get
            {
                return content;
            }
            set
            {
                content = value;
            }
        }

        public DateTime Start
        {
            get
            {
                return start;
            }
            set
            {
                start = value;
            }
        }

        public DateTime End
        {
            get
            {
                return end;
            }
            set
            {
                end = value;
            }
        }

        public bool IsAllDay
        {
            get
            {
                return isAllDay;
            }
            set
            {
                isAllDay = value;
            }
        }

        #endregion

        public bool IsSameEvent(ICalendarItem rhs)
        {
            return Subject.Equals(rhs.Subject) &&
                Location.Equals(rhs.Location) &&
                //Content.Equals(rhs.Content) &&
                Start.Equals(rhs.Start) &&
                End.Equals(rhs.End) && 
                IsAllDay == rhs.IsAllDay;
        }
    }

    public class GoogleCalendarItem : CalendarItem
    {
        protected EventEntry data = null;

        public GoogleCalendarItem(EventEntry data)
        {
            this.data = data;

            this.subject = this.data.Title.Text;
            this.content = this.data.Summary.Text != null ? this.data.Summary.Text : "";
            this.Location = this.data.Locations.Count > 0 ? this.data.Locations[0].ValueString : "";
            if (this.data.Times.Count > 0)
            {
                this.start = this.data.Times[0].StartTime;
                this.end = this.data.Times[0].EndTime;
                this.isAllDay = this.data.Times[0].AllDay; 
            }
            
        }

        public void Update()
        {
            if (data != null)
                data.Update();
        }
    }

}
